package com.example.springboot.project3.controller;

import com.example.springboot.project3.dao.RegionStats;
import com.example.springboot.project3.service.GetDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.io.IOException;
import java.util.List;

@Controller
@RestController
public class GetDataController {
    @Autowired
    GetDataService service = new GetDataService();

    @RequestMapping (value = "/show")
    public ModelAndView showData() throws IOException {
        ModelAndView modelAndView = new ModelAndView();
        List<RegionStats> data = service.getData();
        //统计总感染人数、较昨天新增人数
        Integer totalPeople = 0;
        Integer totalNew = 0;
        for (RegionStats stats :data){
            totalPeople+=stats.getLatestTotalCases();
            totalNew+=stats.getDiffFromPrevDay();
        }
//        System.out.println(data);
        modelAndView.addObject("data", data);
        modelAndView.addObject("totalPeople",totalPeople);
        modelAndView.addObject("totalNew",totalNew);
        modelAndView.setViewName("index");
        return modelAndView;
    }
}
